package com.web.service.novel;

import com.web.bean.category.Category;
import com.web.bean.novel.Novel;
import com.web.dao.category.CategoryDao;
import com.web.dao.category.CategoryDaoImpl;
import com.web.dao.novel.NovelDaoImpl;
import com.web.dao.novel.NovelDao;
import com.web.utils.DBUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * @autor Chen
 * @create 2021--25-14:16
 * @desc
 */
public class NovelServiceImpl implements NovelService {
    @Override
    public List<Novel> selectNovels() throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection=dbUtil.getConnection();
        NovelDaoImpl novelDao=new NovelDaoImpl(connection);
        List<Novel> novels=novelDao.selectNovels();

        CategoryDaoImpl categoryDao=new CategoryDaoImpl(connection);

        List<Novel> collect =novels.stream().map(novel -> {
            try {
                Category category=categoryDao.selectByCode(novel.getNid());
                novel.setNname(category.getCategory());
            }catch (Exception e){
                e.printStackTrace();
            }
            return  novel;
        }).collect(Collectors.toList());

        dbUtil.close();
        return collect;
    }

    @Override
    public Novel selectById(String nid) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        BufferedReader reader=null;
        String temp=null;
        List<String> content=new ArrayList<>();
        int line=1;
        NovelDaoImpl novelDao=new NovelDaoImpl(dbUtil.getConnection());
        Novel novel=novelDao.selectById(nid);
        String nsrc=novel.getNsrc();
        File file=new File(nsrc);
        reader=new BufferedReader(new FileReader(file));
        while ((temp=reader.readLine())!=null){
            content.add(temp);
        }
        dbUtil.close();
        if (reader!=null){
            reader.close();
        }
        return (Novel) content;
    }

    @Override
    public void addNovel(Novel novel) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection=dbUtil.getConnection();
        NovelDaoImpl novelDao=new NovelDaoImpl(connection);
        novelDao.addNovel(novel);
        dbUtil.close();
    }

    @Override
    public void updateNovel(Novel novel) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection=dbUtil.getConnection();
        NovelDaoImpl novelDao=new NovelDaoImpl((connection));
        novelDao.updateNovel(novel);
        dbUtil.close();

    }

    @Override
    public void deleteNovelById(String nid) throws Exception {
        DBUtil dbUtil = DBUtil.getInstance();
        Connection connection=dbUtil.getConnection();
        NovelDaoImpl novelDao=new NovelDaoImpl((connection));
        novelDao.deleteNovelById(nid);
        dbUtil.close();
    }
}
